<template>
  <view>
    <canvas canvas-id="JjjMpqjqkOtrGCmWvpvcWKYSVbrKmPjM" id="JjjMpqjqkOtrGCmWvpvcWKYSVbrKmPjM" class="charts" @touchend="tap"/>
  </view>
</template>

<script>
import uCharts from "../u-charts.js";
var uChartsInstance = {};
export default {
  data() {
    return {
      cWidth: 750,
      cHeight: 500
    };
  },
  onReady() {
    //这里的 750 对应 css .charts 的 width
    this.cWidth = uni.upx2px(750);
    //这里的 500 对应 css .charts 的 height
    this.cHeight = uni.upx2px(500);
    this.getServerData();
  },
  methods: {
    getServerData() {
      //模拟从服务器获取数据时的延时
      setTimeout(() => {
        //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
        let res = {
            categories:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月"],
            series: [
              {
                name: "数据2",
                data: [3500,5000,7100,3000,5500,5100,6500,4500,5100,5000]
              }
             
            ]
          };
        this.drawCharts('JjjMpqjqkOtrGCmWvpvcWKYSVbrKmPjM', res);
      }, 500);
    },
    drawCharts(id,data){
      const ctx = uni.createCanvasContext(id, this);
      uChartsInstance[id] = new uCharts({
        type: "line",
        context: ctx,
        width: this.cWidth,
        height: this.cHeight,
        categories: data.categories,
        series: data.series,
        animation: true,
        background: "#FFFFFF",
        color: ["red"],
        padding: [15,10,0,15],
        legend: {},
        xAxis: {
          disableGrid: true
        },
        yAxis: {
          gridType: "dash",
          dashLength: 2
        },
        extra: {
          line: {
            type: "straight",
            width: 2
          }
        }
      });
    },
    tap(e){
      
      uChartsInstance[e.target.id].showToolTip(e);
    }
  }
};
</script>

<style scoped>
  .charts{
    width: 750rpx;
    height: 500rpx;
  }
</style>